/**
 * 会员中心 我的活动
 */

$(document).ready(function(e) {	

	getlist();
	$("#btn-cancel").click(function(){
		$("DIV[name='memberMyActivity']").show();
		$("DIV[name='viewInfo']").hide();	
	})
	
})

function getlist(currentPage){
	var memberToken = localStorage.getItem('memberToken');
	var requestJson = getSearchParameter(currentPage);
	if(requestJson == false){
		return false;
	}
	$.ajax({
        url: '/euc/_onlineops/getActivity',
        data:requestJson,
        beforeSend: function(request) {
            request.setRequestHeader("token",memberToken );
        },
        //dataType: 'JSON',
        async: true,
        contentType:'application/x-www-form-urlencoded',
        type: 'POST',
        success: function (result, textStatus, response) {
        	if(result.status === -2){
        		gotoLogin();
        	}
        	//alert(JSON.stringify(result));
        	pageBean = result.data;
        	$("#userTable").empty();	    

        	$.each(result.data.list,function(i,data){
        		
        	//if(data.meetingroomStatus ==="启用"){
        		html = '<li onclick="detail('+data.id+')"><div class="listleft"><img src="'+data.url+'" width="272" height="166">'
        		if(data.status>0){
        			html +='<h2 class="st'+data.status+'">'+(data.status==1 ? '报名中' : '')+(data.status==2 ? '进行中' : '')+(data.status==3 ? '已结束' : '')+'</h2>';
        		}
        		html +='</div><div class="listright"><h1>'+data.title+'</h1>';
        		
        		html +='<p>'+data.beginTime+' </p><p>'+data.address+'</p></div>';
        	$("#userTable").append(html);
        	//}
        		
			});
        	$("#totalRow").html(pageBean.totalRow+'&nbsp;');
        	$("#currentPage").html(pageBean.currentPage+'&nbsp;');
        	$("#pageSize").html(pageBean.pageSize+'&nbsp;');        	
        	var pageLength = 15;
        	initailPageNumber(pageBean.totalPage,pageBean.currentPage,pageLength);        	
        	
        },
        error: function () {
        	console.log("error");
        }  
    });
}
function getSearchParameter(currentPage){
	var memberToken = localStorage.getItem('memberToken');

	try{

		if(!currentPage) currentPage=1;
		pn = currentPage;
		pn = pn==0 ? 1 : pn;
		ps =10;
		requestJson ={"pn":pn,"ps":ps,"only_mine":1};
		return requestJson;
	}catch(e){
		console.log(e.messge);
	}
	return false;	
}

function detail(id,day){
	$("DIV[name='memberMyActivity']").hide();
	$("DIV[name='viewInfo']").show();	
	
	var memberToken = localStorage.getItem('memberToken');

	
	requestJson = {"activity_id":id};
	$.ajax({
        url: '/euc/_onlineops/getOneActivity',
        data:requestJson,
        beforeSend: function(request) {
            request.setRequestHeader("token",memberToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/x-www-form-urlencoded',
        type: 'POST',
        success: function (result, textStatus, response) {
        	if(result.status === -2){
        		gotoLogin();
        	}
 
        	data=result.data;
        	html ='<img src="'+data.url+'" width="347" height="232" />';
        	
        	if(data.status>0){
         		html +='<h2 class="st'+data.status+'">'+(data.status==1 ? '报名中' : '')+(data.status==2 ? '进行中' : '')+(data.status==3 ? '已结束' : '')+'</h2>';
        	}
        	$("#d_img").html(html);
        	
        	$("#d_title").html(data.title);
         	$("#d_p1").html('剩余名额 '+data.leftCount);
        	$("#d_p2").html(timestampToTime(data.beginTime));
        	$("#d_p3").html(data.address);
        	$("#d_content").html(data.content);
        },
        error: function () {
        	console.log("error");
        }  
    });
}
function timestampToTime(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    D = date.getDate() + ' ';
    h = date.getHours() + ':';
    m = date.getMinutes() + ':';
    s = date.getSeconds();
    return Y+M+D+h+m+s;
}